using Autofac;
using Codeflows.Tracking;

namespace Codeflows.Contrib.Autofac
{
    public class AutofacCodeflowTrackerFactory : ICodeflowTrackerFactory
    {
        private readonly ILifetimeScope _lifetimeScope;

        public AutofacCodeflowTrackerFactory(ILifetimeScope lifetimeScope)
        {
            _lifetimeScope = lifetimeScope;
        }

        public DisposableContext<ICodeflowTracker> CreateTracker()
        {
            var childScope = _lifetimeScope.BeginLifetimeScope();
            var tracker = childScope.Resolve<ICodeflowTracker>();

            return new DisposableContext<ICodeflowTracker>(tracker, instance => childScope.Dispose());
        }
    }
}